package com.oa.web;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.oa.bean.DepartInfo;
import com.oa.bean.PreContract;
import com.oa.bean.Schedules;
import com.oa.bean.SysFun;
import com.oa.biz.SuperService;
import com.oa.biz.SysFunService;
import com.oa.biz.impl.CommonService;
import com.oa.biz.impl.SchedulesServiceImpl;
import com.oa.biz.impl.SysFunServiceImpl;

@WebServlet(name = "CommonServlet")
public class CommonServlet<T> extends BaseServlet {
	SuperService<T> cDao= new CommonService<T>();
	Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
	
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	T t = getParams(request);
    	boolean isAdded = cDao.add(t);
        response.getWriter().write(String.valueOf(isAdded));
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {              
		//json字符串，前台对象（只有id属性有值）
    	String dataInfo=request.getParameter("data");  	
    	//前台类
    	String typeName=request.getParameter("typeName");
    	Class<?> clazz=null;
    	try {
			clazz=Class.forName("com.oa.bean."+typeName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
        T t = gson.fromJson(dataInfo, (Type)clazz);       
        boolean isUpdate = cDao.modify(t);
        response.getWriter().write(String.valueOf(isUpdate));
    }

    public void getPageDatas(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //前台传送类字符串
    	String typeName=request.getParameter("typeName");
    	System.out.println("查询的实体类型==>"+typeName);
    	//获取对象
//    	Class<?> clazz=null;
    	T t=(T) Class.forName("com.oa.bean."+typeName).newInstance();   
    	List<T> allObj=cDao.findAll(t,null);
//    	System.out.println(allObj.size());
    	response.getWriter().write(gson.toJson(allObj));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//json字符串，前台对象（只有id属性有值）
    	String dataInfo=request.getParameter("data");  	
    	//前台类
    	String typeName=request.getParameter("typeName");
    	
    	Class<?> clazz=null;   	
		clazz=Class.forName("com.oa.bean."+typeName);	
        T t = gson.fromJson(dataInfo, (Type)clazz);
        
        boolean isDeleted=cDao.remove(t);
        response.getWriter().write(String.valueOf(isDeleted));
    }

    public void findById(HttpServletRequest request,HttpServletResponse response) throws Exception{
    	String userInfo=request.getParameter("data");
    	String typeName=request.getParameter("typeName");
    	
    	Class<?> clazz=null;
    	clazz=Class.forName("com.oa.bean."+typeName);
    	T t=gson.fromJson(userInfo, (Type)clazz);
    	
    	T t2=cDao.findById(t);
    	response.getWriter().write(new Gson().toJson(t2));
    }
    
    //crud参数对象
	private T getParams(HttpServletRequest request) {
		//json字符串，前台对象（只有id属性有值）
    	String dataInfo=request.getParameter("data");  	
    	//前台类
    	String typeName=request.getParameter("typeName");
    	Class<?> clazz=null;
    	try {
			clazz=Class.forName("com.oa.bean."+typeName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
        T t = gson.fromJson(dataInfo, (Type)clazz);
		return t;
	}

	//添加日程
	public void addSchedule(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		String scheduleInfo=request.getParameter("schedule");//json字符串，前台的schedule对象
		String preContractInfo=request.getParameter("preContact");//json字符串，前台的preContact对象(只有userId);
//		"beginTime":"2020/08/16 "+schdArr[3]+":00",
//		"endTime":"2020/08/16 "+schdArr[4]+":00",
//		"createTime":'2020/08/16 4:28:09'		//schdArr[8]
//		sInfo.setBeginTime(MyDateUtil.strToTimeStamp(beginTime,"yyyy-MM-dd"));
//		sInfo.setEndTime(MyDateUtil.strToTimeStamp(endTime,"yyyy-MM-dd"));
//		sInfo.setCreateTime(MyDateUtil.strToTimeStamp(createTime,"yyyy-MM-dd"));
		String beginTime=request.getParameter("beginTime");
		String endTime=request.getParameter("endTime");
		String createTime=request.getParameter("createTime");
		
		Schedules sInfo=gson.fromJson(scheduleInfo, Schedules.class);

	
		PreContract pInfo=gson.fromJson(preContractInfo, PreContract.class);
		System.out.println(pInfo);
		boolean isAdded=cDao.addSchedule(sInfo,pInfo,beginTime,endTime,createTime);
		Map<String, String> result=new HashMap<String, String>();
		if (isAdded) {
			result.put("status", "ok");
		}else {
			result.put("status","failed");
		}
		response.getWriter().write(gson.toJson(result));
	}
	
	//根据机构Id,获取所属部门列表
	public void getDeparts(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String branchId=request.getParameter("branchId");
		List<DepartInfo> depts=cDao.getGivenDeparts(branchId);
		response.getWriter().write(gson.toJson(depts));
	}
	
	//搜索部门日程
	public void searchDeptCalendar(HttpServletRequest request,HttpServletResponse response) throws IOException {
//		"branchId":branchId,
//		"deptId":deptId,
//		"userName":userName,
//		"sDate":sDate
		String branchId=request.getParameter("branchId");
		String deptId=request.getParameter("deptId");
		String userName=request.getParameter("userName");
		String sDate=request.getParameter("sDate");
		System.out.println(branchId+"-"+deptId+"-"+userName+"-"+sDate);
		List<Schedules> sList=cDao.searchSchedules(deptId,userName,sDate);
		response.getWriter().write(gson.toJson(sList));
	}

	//获取菜单排序
	public void getOrders(HttpServletRequest request,HttpServletResponse response) throws Exception, IllegalAccessException, ClassNotFoundException {
        //前台传送类字符串
    	String typeName=request.getParameter("typeName");  	
    	//前台传送menuId
    	String menuId=request.getParameter("menuId");
    	SysFunService sService=new SysFunServiceImpl();
    	List<SysFun> ss=new ArrayList<SysFun>();
    	if (menuId.equals("0")) {
			ss=sService.getMenuOrders();
		}else {
			ss=sService.getSubMenuOrders(menuId);
		}
    	response.getWriter().write(gson.toJson(ss));
	}
	
}













